package cn.getech.data.development.service.impl;


import cn.getech.data.development.constant.ScheduleRepeatType;
import cn.getech.data.development.constant.ScheduleType;
import cn.getech.data.development.utils.crontab.ScheduleCronTabUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.intelligence.common.utils.PageUtils2;
import cn.getech.data.intelligence.common.utils.Query;

import cn.getech.data.development.mapper.ScheduleTimeMapper;
import cn.getech.data.development.entity.ScheduleTimeEntity;
import cn.getech.data.development.service.ScheduleTimeService;

@Slf4j
@Service("scheduleTimeService")
public class ScheduleTimeServiceImpl extends ServiceImpl<ScheduleTimeMapper, ScheduleTimeEntity> implements ScheduleTimeService {


    @Override
    public PageUtils2<ScheduleTimeEntity> queryPage(Map<String, Object> params) {
        IPage<ScheduleTimeEntity> page = this.page(
                new Query<ScheduleTimeEntity>().getPage(params),
                new QueryWrapper<ScheduleTimeEntity>()
        );

        return new PageUtils2<ScheduleTimeEntity>(page);
    }

    @Override
    public List<ScheduleTimeEntity> listByTime(Integer scheduleType, Date begin, Date end) {
        Map<String,Object> params = new HashMap<>();
        if(scheduleType != null)
            params.put("scheduleType", scheduleType);
        if(begin != null)
            params.put("beginTime", begin.getTime());
        if(end != null)
            params.put("endTime", end.getTime());
        List<ScheduleTimeEntity> scheduleTimeList = baseMapper.listByTime(params);
        return scheduleTimeList;
    }

    @Override
    public Date getCronNextDateAll(Date currentDate, String cron, Integer scheduleOrInterval) {
        if (StringUtils.isEmpty(cron)) {
            log.error("cron表达式异常：{}", cron);
            return null;
        }
        if(Objects.equals(ScheduleRepeatType.INTERVAL.getCode(),scheduleOrInterval)){
            String[] split = cron.split("&&");
            //转换数据
            if(null != split && split.length >=2){
                try {
                    long time = currentDate.getTime();
                    Long dur = null;
                    if(Objects.equals(split[1],"minute")){
                        dur = Long.valueOf(split[0]) * 60 * 1000;
                    }
                    if(Objects.equals(split[1],"hour")){
                        dur = Long.valueOf(split[0]) * 60 * 60 * 1000;
                    }
                    long durTime = time + dur;
                    return new Date(durTime);
                } catch (Exception e) {
                    log.error("定时任务cron表达式异常cron:{},error：{}", cron, e.getMessage());
                }
            }
            return null;
        }
        try {
            CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
            return cronSequenceGenerator.next(currentDate);
        } catch (Exception e) {
            log.error("定时任务cron表达式异常cron:{},error：{}", cron, e.getMessage());
            return null;
        }
    }

    @Override
    public List<Date> cronTabNextDateList(Date currentDate, String cron) {
        if (StringUtils.isEmpty(cron)) {
            throw new RRException("cron表达式不能为空！");
        }
        List<Date> listData = new ArrayList<>();
        try {
            ScheduleCronTabUtil scheduleCronTabUtil = ScheduleCronTabUtil.create(cron);
            Date[] next = scheduleCronTabUtil.next(currentDate, 10);
            listData.addAll(Arrays.asList(next));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("cronTab表达式无效！error:"+e.getMessage());
        }
        return listData;
    }

    @Override
    public Date cronTabNextDate(Date currentDate, String cron) {
        if (StringUtils.isEmpty(cron)) {
            log.error("cron表达式异常：{}", cron);
            return null;
        }
        try {
            ScheduleCronTabUtil scheduleCronTabUtil = ScheduleCronTabUtil.create(cron);
            return scheduleCronTabUtil.next(currentDate);
        } catch (Exception e) {
            log.error("cronTab表达式无效cron:{},error：{}", cron, e.getMessage());
            return null;
        }
    }

    @Override
    public Date getCronNextDate(Date currentDate, String cron) {
        if (StringUtils.isEmpty(cron)) {
            log.error("cron表达式异常：{}", cron);
            return null;
        }
        try {
            CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
            return cronSequenceGenerator.next(currentDate);
        } catch (Exception e) {
            log.error("定时任务cron表达式异常cron:{},error：{}", cron, e.getMessage());
            return null;
        }
    }
}
